/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function detectCycle(head: ListNode | null): ListNode | null {
  if (head === null) return null
  let fast = head;
  let slow = head;
  let isCycle = false;
  while (fast.next !== null && fast.next.next !== null) {
    fast = fast.next.next
    slow = slow.next!
    if (slow === fast) {
      isCycle = true
      break;
    }
  }
  if (!isCycle) return null
  fast = head
  while (slow!==fast) {
    fast = fast.next!
    slow = slow.next!
  }
  return fast
}
